#!/bin/sh
sudo dd if=/dev/zero of=raspberrypi.img bs=1MB count=3000
# sudo watch -n 5 pkill -USR1 ^dd


sudo parted raspberrypi.img --script -- mklabel msdos
sudo parted raspberrypi.img --script -- mkpart primary fat32 8192s 122879s
sudo parted raspberrypi.img --script -- mkpart primary ext4 122880s -1

loopdevice=`sudo losetup -f --show raspberrypi.img`
echo "loopdevice=$loopdevice"
device=`sudo kpartx -va $loopdevice | sed -E 's/.*(loop[0-9])p.*/\1/g' | head -1`
echo "device=$device"
device="/dev/mapper/${device}"
echo "device=$device"
partBoot="${device}p1"
partRoot="${device}p2"
echo "partBoot=$partBoot partRoot=$partRoot"
sudo mkfs.vfat $partBoot
sudo mkfs.ext4 $partRoot
sudo mount -t vfat $partBoot /media
df -h
ls /media
sudo cp -rfp /boot/* /media/
echo "partBoot: ========>"
ls /media
sudo umount /media
sudo mount -t ext4 $partRoot /media/
df -h
ls /media
cd /media
sudo dump -0uaf - / | sudo restore -rf -
echo "partRoot: ========>"
cd
sudo umount /media
sudo kpartx -d $loopdevice
sudo losetup -d $loopdevice